//
//  JJNewFeatureViewController.swift
//  微博1
//
//  Created by 卷卷 on 15/9/8.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit

//可重用表示符号
private let JJNewFeatureCellID = "JJNewFeatureCellID"
//新特性图片数量
private let JJNewFeatureCount = 4

class JJNewFeatureViewController: UICollectionViewController {

    //实现 init（）构造函数，方便外部的代码调用，不需要额外指定布局属性
    init(){
        //调用父类的默认构造函数
        super.init(collectionViewLayout:UICollectionViewFlowLayout())
    }
    
    required init(coder aDecoder: NSCoder) {
       
        fatalError("init(coder:)has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        // 注册可重用的 cell 
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: JJNewFeatureCellID)

        //准备布局
        prepareLayout()
    }

   //1.准备布局
    private func prepareLayout (){
       
        //获得当前布局属性
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = view.bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    //MARK : UICollectionDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return JJNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(JJNewFeatureCellID, forIndexPath: indexPath) as! JJNewFeatureCell
    
        // Configure the cell
        cell.imageIndex = indexPath.item
    
        return cell
    }
    
    //indexPath 参数是之前的cell的 indexPath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    
        //取当前的 cell 的 indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
        //判断是否是最后一个
        if path.item == JJNewFeatureCount - 1 {
            
            //获取cell
            let cell = collectionView.cellForItemAtIndexPath(path) as! JJNewFeatureCell
            
            cell.showStartButton()
        }
        
    }
   
}

//新特性cell，private保证cell只能被当前控制器使用，在当前文件中，所有的private 都是摆设
private class JJNewFeatureCell:UICollectionViewCell{
    
    //图像索引属性
    private var imageIndex: Int = 0 {
        didSet{
            
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
        }
    }
    
        //开始点击按钮，如果是类是 private 的，即使没有对方法进行修饰，运行循环同样无法调用监听按钮
        func clicksStartButton() {
        
            NSNotificationCenter.defaultCenter().postNotificationName(JJSwitchRootViewControllerNotification, object: nil)
        }
    
   //动画显示启动按钮
    private func showStartButton() {
       
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //Damping:弹性系度，0～1，越小越弹
        //Velocity:初始速度
        UIView.animateWithDuration(1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
        }
    }
    
    //frame 的大小来自于 layout 的 itemSize
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init(coder aDecoder: NSCoder) {
        
        super.init(coder:aDecoder)
        
        setupUI()
    }
    
    //设置界面元素
    private func setupUI(){
        
        print(bounds)
       
        //1.添加控件
        addSubview(iconView)
        addSubview(startButton)
        
        //2.指定布局
        iconView.frame = bounds
        
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //自动布局的约束是添加在父视图上的
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
    }
    
    //MARK：－ 懒加载
    //图像视图
     private lazy var iconView = UIImageView()
    
    //开始体验按钮
    private lazy var startButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
